package com.example.learn_gl_android.anglib.util.math

data class Vector3(val elements: FloatArray = FloatArray(3)) : Cloneable {

    companion object {
        fun one() : Vector3 {
            val ret = Vector3()
            ret.elements[0] = 1f
            ret.elements[1] = 1f
            ret.elements[2] = 1f

            return ret
        }

        fun cross(v1: Vector4, v2 : Vector4) : Vector4{
            val ret = Vector4()

            val a = v1.elements
            val b = v2.elements

            ret.elements[0] = a[1] * b[2] -a[2] * b[1]
            ret.elements[1] = a[2] * b[0] -a[0] * b[2]
            ret.elements[2] = a[0] * b[1] -a[1] * b[0]
            return ret;
        }

        fun dot(v1: Vector3, v2 : Vector3) : Float{
            val a = v1.elements
            val b = v2.elements

            return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
        }
    }

    operator fun plus (other : Vector3) : Vector3{

        val ret = Vector3()
        ret.elements[0] = elements[0] + other.elements[0]
        ret.elements[1] = elements[1] + other.elements[1]
        ret.elements[2] = elements[2] + other.elements[2]

        return ret
    }
    operator fun plusAssign (other : Vector3) {
        elements[0] = elements[0] + other.elements[0]
        elements[1] = elements[1] + other.elements[1]
        elements[2] = elements[2] + other.elements[2]

    }

    operator fun minus (other : Vector3) : Vector3{
        val ret = Vector3()
        ret.elements[0] = elements[0] - other.elements[0]
        ret.elements[1] = elements[1] - other.elements[1]
        ret.elements[2] = elements[2] - other.elements[2]

        return ret
    }

    operator fun minusAssign (other : Vector3) {
        elements[0] = elements[0] - other.elements[0]
        elements[1] = elements[1] - other.elements[1]
        elements[2] = elements[2] - other.elements[2]

    }


    override fun clone(): Vector4 {
        return Vector4(elements.clone())
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Vector3

        return elements.contentEquals(other.elements)
    }

    override fun hashCode(): Int {
        return elements.contentHashCode()
    }

}